package com.luge.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.luge.heart.R;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings.Secure;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

public class Helper {
	public static ProgressDialog mDialog;

	public static void log(String string) {
		Log.i("CHappyStudy", string);
	}

	public static void log(String tag, String string) {
		Log.i(tag, string);
	}

	public static void toast(Context context, String string) {
		Toast.makeText(context, string, Toast.LENGTH_LONG).show();
	}

	public static void toast(Context context, int resid) {
		Toast.makeText(context, resid, Toast.LENGTH_LONG).show();
	}

	public static void showDialog(Context context) {
		mDialog = new ProgressDialog(context);
		mDialog.setMessage(context.getString(R.string.request_data));
		mDialog.setCanceledOnTouchOutside(false);
		mDialog.show();
	}

	public static void showDialog(Context context, String message) {
		mDialog = new ProgressDialog(context);
		mDialog.setMessage(message);
		mDialog.setCanceledOnTouchOutside(false);
		mDialog.show();
	}

	public static void showDialog(Context context, String title, String message) {
		mDialog = new ProgressDialog(context);
		mDialog.setTitle(title);
		mDialog.setMessage(message);
		mDialog.setCanceledOnTouchOutside(false);
		mDialog.show();
	}

	public static void removeDialog() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
	}

	public static boolean netState(Context context) {
		try {
			ConnectivityManager connectivity = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) {
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected()) {
					if (info.getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		} catch (Exception e) {

		}
		return false;
	}

	public static boolean regexTelephone(String string) {
		Pattern pattern = Pattern.compile("(13[0-9]|15[0-9]|18[0-9])\\d{8}");
		Matcher matcher = pattern.matcher(string);
		return matcher.matches();
	}

	public static String utf_8(String string) {
		try {
			string = URLEncoder.encode(string, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			string = "";
		}
		return string;
	}

	public static String decodeUTF8(String string) {
		try {
			string = URLDecoder.decode(string, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			string = "";
		}
		return string;
	}

	public static int getDisplayWidth(Context context) {
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		return metrics.widthPixels;
	}

	public static int getDispalyHeight(Context context) {
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		return metrics.heightPixels;
	}

	public static String getDeviceId(Context context) {
		return Secure
				.getString(context.getContentResolver(), Secure.ANDROID_ID);
	}

	public static List<PackageInfo> getAllApps(Context context) {
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager manager = context.getPackageManager();
		List<PackageInfo> paklist = manager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) {
			PackageInfo pak = (PackageInfo) paklist.get(i);
			if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
				apps.add(pak);
			}
		}
		return apps;
	}

	public static void hideInputMethod(Context context, EditText edit) {
		((InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE))
				.hideSoftInputFromWindow(edit.getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
	}

}
